import {
  request
} from "../../request/request";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    productsList: [],
    rightContent: [],
    selectIndex: 0,
    scroll_top: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  getProductsList: async function (e) {
    const productsList = await request({
      url: "/categories",
    });
    //进入页面渲染index为0得数据
    const rightContent=productsList[0].children
    const selectIndex=0
    // 存储数据
    wx.setStorageSync('cate', {data:productsList, time:Date.now()})
    this.setData({productsList,rightContent,selectIndex});
  },
  // 点击左边得标题，显示右边得对应内容
  handleItemClick: function (e) {
    const index = e.currentTarget.dataset.index
    const scroll_top=0
    this.setData({
      selectIndex: index,
      scroll_top:scroll_top,
      rightContent:this.data.productsList[index].children
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //获取storage里面得数据
    let cate=wx.getStorageSync('cate')
    if(!cate){
      //如果设置时间过期，重新获取数据
      this.getProductsList();
    }else {
      if(Date.now()-cate.time>60 * 60 * 1000){
        this.getProductsList();
      }else{
        let  productsList=cate.data
        // console.log(productsList)
        const rightContent=productsList[0].children
        const selectIndex=0
        this.setData({productsList,rightContent,selectIndex})
      }
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});